<template>
  <KiFormDialog :show.sync="dialogVisible" title="物流信息" readonly>
    <orderInfo v-model="logistics" />

    <hr />
    <!-- 物流形式 -->
    <span v-if="logisticsName">
      <i class="el-icon-s-promotion"></i>
      物流:
      <b>{{ logisticsName }}</b>
    </span>
    <span v-else-if="type == 1">无需物流</span>
    <span v-else-if="type == 2">上门自提</span>
    <span v-else-if="type == 3">同城跑腿</span>
    <span v-else>未选择物流</span>

    <!-- 物流单号 -->
    <span v-if="logisticsNum" class="ml-20px">
      <b>{{ logisticsNum }}</b>
    </span>
    <el-button
      title="编辑物流"
      icon="el-icon-edit-outline text-20px ml-5px"
      type="text"
      @click="edit"
      v-if="isMerchant"
    ></el-button>

    <el-table v-if="type == 0" :data="tableData" border v-loading="loading">
      <el-table-column label="物流信息" prop="status"></el-table-column>
      <el-table-column
        label="更新时间"
        prop="time"
        width="160"
      ></el-table-column>
    </el-table>
  </KiFormDialog>
</template>
<script>
import API from './api'
import orderInfo from './_orderInfo'
import { mapGetters } from 'vuex'
export default {
  components: {
    orderInfo
  },
  data () {
    return {
      dialogVisible: false,
      tableData: [],
      loading: false,
      logisticsNum: '', //物流单号
      logisticsName: '', //物流公司
      logistics: null
    }
  },
  computed: {
    type () {
      //物流类型
      if (this.logistics && this.logistics.receiptInfo) {
        return this.logistics.receiptInfo.type
      }
      return null
    },
    ...mapGetters(['userInfo']),
    isMerchant () {
      //是否商户
      return this.userInfo.userType == 1
    }
  },
  methods: {
    open (row) {
      this.dialogVisible = true
      this.logisticsNum = row.logisticsNum
      this.tableData = []

      // 查物流记录
      this.loading = true
      if (this.logisticsNum) {
        API.queryExpress({
          numberCode: this.logisticsNum,
          phone: row.purchaserPhone
        })
          .then(({ data }) => {
            this.tableData = data
          })
          .finally(() => {
            this.loading = false
          })
      }

      // 获取物流公司名称
      API.getOrderInfo({
        orderId: row.orderId
      }).then(({ data }) => {
        this.logistics = data
        if (data.receiptInfo) {
          this.logisticsName = data.receiptInfo.logisticsCompany
        }
      })
    },
    edit () {
      this.dialogVisible = false
      this.$parent.$refs.deliverPage.edit(this.logistics.orderId)
    }
  }
}
</script>
